﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLogicLayer
{
  public  class DepartmentFacade
    {
        InventorySoreDataContext context;
        Department department;
        EmployeeFacade ef = new EmployeeFacade();
        public DepartmentFacade() 
        {
            context = new InventorySoreDataContext();
        }
        public IQueryable FindAll()
        {
            var departments = from depart in context.Departments select depart;
            return departments;
        }
        public IQueryable FindAllDepartmentAndPriority()
        {
            var data = from dept in context.Departments
                       where dept.DepartmentCode!="STNT"
                        select new
                        {
                            DepartmentName=dept.DepartmentName,
                            DepartmentCode = dept.DepartmentCode,
                            PriorityName= dept.Priority.PriorityName,
                            PriorityID=dept.PriorityID,
                        };

                return data;
        }

        public IQueryable FindAllPriority()
        {
            var data = from prio in context.Priorities
                       select new
                           {
                               PriorityName = prio.PriorityName,
                               PriorityID = prio.PriorityID,
                           };
            return data;
        }
        public Department FindDepartmentByDepartmentID(string departmentcode)
        {
            var onedepart=(from depart in context.Departments where depart.DepartmentCode==departmentcode select depart).Single();
            return onedepart;
        }
        public string GetCollectionPoint(string deptID)
        {
            var onedepart = (from collection in context.Departments where collection.DepartmentCode == deptID select collection).Single();
            return onedepart.CollectionPointID;
        }
        public IQueryable GetCollectionPointAndRepresentative(string deptID)
        {
            var onedepart = (from collection in context.Departments where collection.DepartmentCode == deptID 
                             select new {EmployeeName= collection.Employee.EmployeeName,CollectionPoint=collection.CollectionPoint.Location, });
            return onedepart;
        }
        
        public void UpdateCollectionID(string departmentID,string collectionPointID)
        {
           department=FindDepartmentByDepartmentID(departmentID);
           department.CollectionPointID = collectionPointID;
           department.CollectionPoint.CollectionPointID = collectionPointID;
           context.SubmitChanges();

        }

        public void UpdateRepresentativeRole(string departmentID, string representativeID)
        {
            department = FindDepartmentByDepartmentID(departmentID);
            department.RepresentativeID = representativeID;
            Employee emp= ef.FindEmployeeByID(representativeID);
            emp.RoleId = "3";
            emp.EmployeeRole.RoleId = "3";
            context.SubmitChanges();
        }

        public void UpdatePriorityID(string departmentID, string priorityID)
        {
            department = FindDepartmentByDepartmentID(departmentID);
            department.PriorityID = priorityID;
            department.Priority.PriorityID = priorityID;
            context.SubmitChanges();
        }

        public void setTempRep(string depID, string tempRepID, DateTime stDate, DateTime endDate)
        {
            department = FindDepartmentByDepartmentID(depID);
            department.TempRepID = tempRepID;
            department.TempStDate = stDate;
            department.TempEdDate = endDate;
            context.SubmitChanges();
        }

        public IQueryable SearchEmployees(string empID)
        {
            Employee emps = new EmployeeFacade().FindEmployeeByID(empID);
            string depID = emps.Departmentcode;
            var dep = from emp in context.Employees where emp.Departmentcode == depID && emp.RoleId == "2" select emp;
            return dep;

        }

        public static void Main(string[] args)
        {
            var result = new DepartmentFacade().SearchEmployees("A0102939A");
            foreach (Employee employee in result)
            {
                Console.WriteLine(employee.EmployeeName);
            }
            Console.ReadLine();
        }

        public void UpdateContactName(string departmentID, string ContactName)
        {
            department = FindDepartmentByDepartmentID(departmentID);
            department.ContactName = ContactName;
            context.SubmitChanges();
        }
        //public static void Main(string[] args)
        //{
        //    var result = new DepartmentFacade().FindAll();
        //    foreach (Department depart in result)
        //    {
        //        Console.WriteLine(depart.DepartmentCode + " " + depart.DepartmentName);
        //    }
        //    Console.ReadLine();
        //}

        public string getDepartmentCode(string employeeID)
        {
            var employee = (from em in context.Employees where em.EmployeeID == employeeID select em).Single();
            return employee.Departmentcode;
        }

        public IQueryable FindDepartmentByClerk(string clerkID)
        {
        var departments = from depart in context.Departments where depart.ClerkID == clerkID select depart;
        return departments;
        }

        public String FindClerkIdByDeptCode(string deptCode)
        {
            var cId = (from dept in context.Departments where dept.DepartmentCode == deptCode select dept).Single();
            return cId.ClerkID;
        }

        
    }
}
